---@class components
---@field staff_arachnidnexus_command component_staff_arachnidnexus_command

-- local function on_val(self, value)
    -- self.inst.replica.staff_arachnidnexus_command:SetVal(value)
-- end

---@class component_staff_arachnidnexus_command
---@field inst ent
---@field _choose_elf ent_webbert_skitter|nil # 选择的elf
local staff_arachnidnexus_command = Class(
---@param self component_staff_arachnidnexus_command
---@param inst ent
function(self, inst)
    self.inst = inst
    -- self.val = 0
end,
nil,
{
    -- val = on_val,
})

-- function staff_arachnidnexus_command:OnSave()
--     return {
--         -- val = self.val
--     }
-- end

-- function staff_arachnidnexus_command:OnLoad(data)
--     -- self.val = data.val or 0
-- end

local function CreateHead(tar_food)
    local _ds = tar_food and tar_food:IsValid() and tar_food:GetDebugString() or nil
    if _ds then
        local bank, build, anim = _ds:match("bank: (%S+) build: (%S+) anim: (%S+)")
        local fx = SpawnPrefab('fx_webbert_skitter_head')
        fx.AnimState:SetBank(bank)
        fx.AnimState:SetBuild(build)
        fx.AnimState:PlayAnimation(anim)
    end
end

local bank_isoverridesymbol = {
    wobster_sheller_dead = 'lobster'
}

---comment
---@param food ent
---@param doer ent
function staff_arachnidnexus_command:DoActionSpawnOneElf(food,doer)
    if food.components.edible then
        local isoverridesymbol = food:HasTag('preparedfood')  and food.prefab or nil

        local hunger = food.components.edible.hungervalue or 0
        local san = food.components.edible.sanityvalue or 0
        local hp = food.components.edible.healthvalue or 0

        local x,y,z = food:GetPosition():Get()

        SpawnPrefab('monkey_morphin_power_players_fx').Transform:SetPosition(x,y,z)

        local elf = SpawnPrefab('webbert_skitter')
        elf.Transform:SetPosition(x,y,z)

        local info = food:GetDebugString()
        if info then
            local bank, build, anim = info:match("bank: (%S+) build: (%S+) anim: (%S+)")
            if bank and build and anim then
                elf.components.webbert_skitter_misc:ExchangeHead(isoverridesymbol,bank_isoverridesymbol[food.prefab] or bank, build, anim)
            end
        end
        self.inst.components.staff_arachnidnexus_bind:AddOneChild(elf)
        -- 最大血量
        local maxhp = math.max(20,hunger > 0 and (hunger * math.max(.6,math.random()*4) * (food:HasTag('edible_MEAT') and 2 or 1)) or 20)
        -- 每?秒回复血量(可能为负)
        local hp_recovery = hp*.2
        -- 给玩家回复血量
        local hp_recovery_player = math.max(1, san > 0 and (san * .2) or 1)

        elf.components.webbert_skitter_misc:Set3SizeWhenSpawn(maxhp,hp_recovery,hp_recovery_player)

        SUGAR_webber_the_trainer:consumeOneItem(food)

        if doer.components.sanity then
            doer.components.sanity:DoDelta(10,true)
            x,y,z = doer:GetPosition():Get()
            SpawnPrefab('abigail_rising_twinkles_fx').Transform:SetPosition(x,1.5,z)
        end
    end
    return true
end

---待命
---@param pt Vector3
---@return boolean
function staff_arachnidnexus_command:DoActionStandBy(pt)
    local childs = self.inst.components.staff_arachnidnexus_bind:GetChildren()
    ---@cast childs ent_webbert_skitter[]

    local x,_,z = pt:Get()
    local pointgener = COORDS_webber_the_trainer:GenPointInCircle(x,z,7)
    for _,v in pairs(childs or {}) do
        if v and v:IsValid() and v.components.webbert_skitter_misc and not v.components.webbert_skitter_misc:HasTempMaster() then -- 注意没有临时主人的才可以被我们命令
            v.components.webbert_skitter_misc:StopThinking(5)
            v.components.locomotor:Stop()
            local res_x,res_z = pointgener()
            SpawnPrefab('shadow_puff_solid').Transform:SetPosition(res_x,0,res_z)
            v.Transform:SetPosition(res_x,0,res_z)
        end
    end
    return true
end

---召回
---@param player ent
---@return boolean
function staff_arachnidnexus_command:DoActionCallBack(player)
    local childs = self.inst.components.staff_arachnidnexus_bind:GetChildren()
    ---@cast childs ent_webbert_skitter[]

    local x,_,z = player:GetPosition():Get()
    local pointgener = COORDS_webber_the_trainer:GenPointInCircle(x,z,4)
    for _,v in pairs(childs or {}) do
        if v and v:IsValid() and v.components.webbert_skitter_misc and not v.components.webbert_skitter_misc:HasTempMaster() then -- 注意没有临时主人的才可以被我们命令
            v.components.locomotor:Stop()
            local res_x,res_z = pointgener()
            SpawnPrefab('shadow_puff_solid').Transform:SetPosition(res_x,0,res_z)
            v.Transform:SetPosition(res_x,0,res_z)

            v.components.webbert_skitter_misc:StartThinking()
        end
    end
    return true
end

---comment
---@param player ent
---@param content string
function staff_arachnidnexus_command:_Say(player,content)
    if player and player:IsValid() and player.components.talker then
        player.components.talker:Say(content)
    end
end

---选择elf以给队友
---@param elf ent_webbert_skitter
---@param doer ent
---@return boolean
function staff_arachnidnexus_command:DoActionChooseElf(elf,doer)
    -- 检查该elf是不是该手杖的elf
    local ismychild = false
    local childs = self.inst.components.staff_arachnidnexus_bind and self.inst.components.staff_arachnidnexus_bind:GetChildren()
    for _,child in pairs(childs or {}) do
        if child == elf then
            ismychild = true
            break
        end
    end
    if not ismychild then
        self:_Say(doer,STRINGS.MOD_WEBBER_THE_TRAINER.ACTIONS.ACTION_WEBBERT_ELF_COMMAND_SETTEMPMASTER_ELF_failed_for_notmychild)
        return true
    end
    self._choose_elf = elf
    return true
end

---将选择的elf给队友
---@param player ent
---@param doer ent
---@return boolean
function staff_arachnidnexus_command:DoActionChoosePlayer(player,doer)
    if self._choose_elf == nil or not self._choose_elf:IsValid() then
        self:_Say(doer,STRINGS.MOD_WEBBER_THE_TRAINER.ACTIONS.ACTION_WEBBERT_ELF_COMMAND_SETTEMPMASTER_PLAYER_failed_for_notchooseelf)
        return true
    end
    local elf = self._choose_elf
    if elf and elf.components.webbert_skitter_misc then
        elf.components.webbert_skitter_misc:SetTempMaster(player)
    end
    return true
end

return staff_arachnidnexus_command